// Copyright Notice:
//				tab.js v1.0.2
//			Copyright@2008 - OpenSiteMobile
//				All rights reserved
// ==========================================================================
//			http://www.opensitemobile.com
// ==========================================================================
// Contact Information:
//			Author: Dwight Vietzke
//			Email:  dwight_vietzke@yahoo.com

/*
	OpenSiteMobile simple tab generator w/dojo integration
*/

dojo.provide("msos.tab");
dojo.require("msos.common");
dojo.require("dojo.cookie");


msos.tab.tool = function(tabs_div) {
 this.tab_div_el = tabs_div;
 this.tab_set_active = 1;
 this.act_tab_style = 'tab_active';
 this.pas_tab_style = 'tab_passive';
 this.com_tab_style = 'tab_common';
 this.tab_cookie_name = 'tab_index_' + tabs_div.id;
 this.tab_height_px = 250;
 this.tab_onclick = null;
 this.tabs = new Array;
 var tab_obj = this;

 this.add_tab = function(def_obj) {
       def_obj.container.style.height = tab_obj.tab_height_px + 'px';
       var tab_span = document.createElement("span");
       var tab_numb = tab_obj.tabs.length + 1;
       tab_span.id = 'tab_span_' + tab_numb;
       tab_span.title = def_obj.tab_title;
       tab_span.unselectable = "on";
       tab_span.container = def_obj.container;

       dojo.connect(tab_span, "onclick", null, tab_obj.go_to_tab);

       var tab_caption = document.createTextNode(def_obj.caption);
       tab_span.appendChild(tab_caption);
       tab_obj.tabs.push(tab_span);
 }

 this.generate_tabs = function() {
       for (var i = 0; i < tab_obj.tabs.length; i++) {
              tab_obj.tab_div_el.appendChild(tab_obj.tabs[i]);
       }
       // Use current 'tab_set_active' tab
       tab_obj.set_tab();
 }

 this.get_tab_by_cookie = function() {
       var tab_index = dojo.cookie(tab_obj.tab_cookie_name);
       console.debug('get_tab_by_cookie -> index: ' + tab_index + ' for cookie: ' + tab_obj.tab_cookie_name);
       if (tab_index)  tab_obj.tab_set_active = tab_index;	
 }

 this.set_tab = function() {
       var set_act_tab = tab_obj.tab_set_active || 1;
       if (set_act_tab == 'na')		set_act_tab = 1;
       if (isNaN(parseInt(set_act_tab)))	set_act_tab = 1;
       if (set_act_tab < 1)			set_act_tab = 1;
       if (set_act_tab > tab_obj.tabs.length)	set_act_tab = tab_obj.tabs.length;
       for (var i = 0; i < tab_obj.tabs.length; i++) {
	      if (set_act_tab == i + 1) {
                     tab_obj.tabs[i].className = tab_obj.act_tab_style + " " + tab_obj.com_tab_style;
		     tab_obj.tabs[i].container.style.display = 'block';
		     tab_obj.tab_set_active = i + 1;
		     dojo.cookie(tab_obj.tab_cookie_name, i + 1, { expires:1 });
                     console.debug('set_tab -> index: ' + (i + 1) + ' for cookie: ' + tab_obj.tab_cookie_name);

	      } else {
		     tab_obj.tabs[i].className = tab_obj.pas_tab_style + " " + tab_obj.com_tab_style;
		     tab_obj.tabs[i].container.style.display = 'none';
	        }
       }
 }

  // --------------------------
  // Event Functions
  // --------------------------

 this.go_to_tab = function(evt) {
       evt.preventDefault();
       evt.stopPropagation();
       var target = evt.target;
       var tab_idx = tab_obj.tab_set_active;
       var tab_id = target.id.replace(/tab_span_/, '');
       if (target) tab_idx = parseInt(tab_id) || tab_idx;
       msos.common.event_debug(evt, "\nTab go_to_tab - index: " + tab_idx);
       tab_obj.tab_set_active = tab_idx;
       tab_obj.set_tab();
       if (typeof(tab_obj.tab_onclick) == 'function') tab_obj.tab_onclick();
 }

}